﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace AlbumDownloader
{
    public partial class frmMain : Form
    {
        private bool downloading = false;
        private int picnum = 0;
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            webXiaonei.Navigate("www.xiaonei.com");
        }

        private void webXiaonei_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            txtURL.Text = webXiaonei.Url.ToString();
        }

        private void webXiaonei_NewWindow(object sender, CancelEventArgs e)
        {
            webXiaonei.Navigate(webXiaonei.StatusText);
            txtURL.Text = webXiaonei.Url.ToString();
            e.Cancel = true;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            webXiaonei.Navigate(txtURL.Text);
        }
        private void nextpage()
        {
            HtmlElementCollection hec = webXiaonei.Document.GetElementsByTagName("a");
            foreach (HtmlElement he in hec)
            {
                if (he.InnerText == "下一张")
                {
                    webXiaonei.Navigate(he.GetAttribute("href"));
                    break;
                }
            }
        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            lisImages.Items.Clear();
            lisImages.Items.Add(webXiaonei.Document.GetElementById("photo").GetAttribute("src"));
            label1.Text = "已分析";
            dgvList.ColumnCount = 3;
            dgvList.Rows.Clear();
            dgvList.Rows.Add(new String[3] { webXiaonei.Document.GetElementById("title").GetAttribute("value"), webXiaonei.Document.GetElementById("photo").GetAttribute("src"), webXiaonei.Url.ToString() });
            lblCount.Text = "1";
            btnStop.Enabled = true;
            btnStart.Enabled = false;
            downloading = true;
            nextpage();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            downloading = false;
            btnStop.Enabled = false;
            btnStart.Enabled = true;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            FileStream fi = new FileStream("output.txt", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fi);
            for (int i = 0; i < lisImages.Items.Count; i++)
            {
                sw.WriteLine(lisImages.Items[i]);
            }
            sw.Flush();
            sw.Close();
            Process pr = new Process();
            pr.StartInfo.FileName = "notepad.exe";
            pr.StartInfo.Arguments = "output.txt";
            pr.Start();
            pr.Close();
        }

        private void webXiaonei_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.FindForm().Text = "校内相册下载器" + " - " + webXiaonei.DocumentTitle;
            txtURL.Text = webXiaonei.Url.ToString();
            //if (webXiaonei.DocumentTitle == "") return;
            //if (webXiaonei.DocumentTitle == "校内网 - 浏览照片")
            if (webXiaonei.Url.ToString().Contains("getphoto"))
            {
                Console.WriteLine(webXiaonei.Url.ToString());
                if (!btnStop.Enabled) btnStart.Enabled = true;
                HtmlElementCollection hec = webXiaonei.Document.GetElementsByTagName("div");
                foreach (HtmlElement he in hec)
                {
                    String strTemp = he.InnerText;
                    if (strTemp.Contains("第") && strTemp.Contains("张 / 共"))
                    {
                        String strCurr = strTemp.Substring(strTemp.LastIndexOf("第", strTemp.IndexOf("张 / 共")), strTemp.IndexOf("张", strTemp.IndexOf("张 / 共") + 1) - strTemp.LastIndexOf("第", strTemp.IndexOf("张 / 共")) + 1);
                        strCurr = strCurr.Substring(strCurr.IndexOf("/") + 2);
                        lblCurr.Text = strCurr;
                        picnum = Convert.ToInt32(strCurr.Substring(1, strCurr.Length - 2));
                        break;
                    }
                }
                Console.WriteLine(downloading);
                if (downloading)
                {
                    Console.WriteLine(downloading);
                    //String pic = webXiaonei.Document.GetElementById("photo").GetAttribute("src");
                    HtmlElement hd = webXiaonei.Document.GetElementById("photo");
                    //HtmlElement he = webXiaonei.Document.GetElementById("picurl_0");
                    //if ((hd != null) || (he != null))
                    //{
                    //if (hd == null) hd = he;
                    String va = hd.GetAttribute("src");
                    String pic = va;
                    if (Convert.ToInt32(lblCount.Text) == picnum)
                    {
                        MessageBox.Show("Finished!");
                        downloading = false;
                        btnStop.Enabled = false;
                        btnStart.Enabled = true;
                    }
                    if (!lisImages.Items.Contains(pic))
                    {
                        lisImages.Items.Add(pic);
                        dgvList.Rows.Add(new String[3] { webXiaonei.Document.GetElementById("title").GetAttribute("value"), pic, webXiaonei.Url.ToString() });
                        lblCount.Text = (Convert.ToInt32(lblCount.Text) + 1).ToString();
                    }
                    if (downloading) nextpage();
                    //}
                }
            }
            else
            {
                btnStart.Enabled = false;
                btnStop.Enabled = false;
            }
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (downloading) return;
            webXiaonei.Navigate(dgvList.Rows[e.RowIndex].Cells[2].Value.ToString());
        }
    }
}
